﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;

namespace SharpGeo
{
    /// <summary>
    /// Represents a Coordinate Reference System
    /// </summary>
    public abstract class CoordinateReferenceSystem
    {
        //******************************************************************************
        //** Note this class is currently empty and represents a future feature
        //** For now this class only supports the WGS84 Coordinate Reference System
        //******************************************************************************

        #region Constructor

        /// <summary>
        /// The protected constructor
        /// </summary>
        protected CoordinateReferenceSystem()
        {
        }

        #endregion Constructor

        #region Properties

        /// <summary>
        /// Gets the name of the Coordinate Reference System
        /// </summary>
        public string Name { get; protected set; }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Returns a CoordinateReferenceSystem parsed from Well Known Text (not implemented)
        /// </summary>
        /// <param name="wellKnownText">The Well Known Text to parse</param>
        /// <returns></returns>
        public static CoordinateReferenceSystem CreateFromWellKnownText(string wellKnownText)
        {
            //not yet implemented fail silently
            return new Wgs84();
        }

        /// <summary>
        /// Returns a CoordinateReferenceSystem representing WGS84
        /// </summary>
        /// <returns>A CoordinateReferenceSystem representing WGS84</returns>
        public static CoordinateReferenceSystem LoadWgs84()
        {
            return new Wgs84();
        }

        /// <summary>
        /// Calculates the distance between two ICoordinates using the Haversine formula
        /// </summary>
        /// <param name="coordinate1">The first ICoordinate</param>
        /// <param name="coordinate2">The second ICoordinate</param>
        /// <returns>The distance between the two ICoordinate using the Haversine formula</returns>
        public virtual double CalculateDistance(ICoordinate coordinate1, ICoordinate coordinate2)
        {
            if (coordinate1 == null || coordinate2 == null)
                return double.NaN;

            var ph1 = coordinate1[0] * Unit.DegreesToRadians;

            var th1 = coordinate1[1] * Unit.DegreesToRadians;
            var th2 = coordinate2[1] * Unit.DegreesToRadians;

            var dz = Math.Sin(th1) - Math.Sin(th2);
            var dx = Math.Cos(ph1) * Math.Cos(th1) - Math.Cos(th2);
            var dy = Math.Sin(ph1) * Math.Cos(th1);

            return Math.Asin(Math.Sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * 6372800;
        }

        #endregion Methods

        #region WGS84

        private class Wgs84 : CoordinateReferenceSystem
        {
            public Wgs84()
            {
                Name = "WGS84";
            }
        }

        #endregion WGS84
    }
}